package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleSearchService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ArticleController
 * @Description TODO
 * @Author zhangshuai
 * @Date 2019/1/16 17:26
 * @Version 1.0
 **/

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * 增加索引
     *
     * @param article
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(Article article) {
        articleSearchService.add(article);

        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 检索
     *
     * @param keywords
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/search/{keywords}/{page}/{size}", method = RequestMethod.GET)
    public Result findByTitleLike(@PathVariable String keywords, @PathVariable int page, @PathVariable int size) {
        Page<Article> articles = articleSearchService.findByTitleLike(keywords, page, size);
        return new Result(true, StatusCode.OK, "检索成功", new PageResult<>(articles.getTotalElements(), articles.getContent()));
    }

}
